import { Injectable } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

import { FormItemBase } from '../../domain/form/form-item-base';

@Injectable({
  providedIn: 'root'
})
export class FormItemControlService {
  constructor() { }

  toFormGroup(formItems: FormItemBase<string>[] ) {
    const group: any = {};

    formItems.forEach(formItem => {
      group[formItem.key] = formItem.required ? new FormControl(formItem.value || '', Validators.required)
                                              : new FormControl(formItem.value || '');
    });
    return new FormGroup(group);
  }
}
